<?php
//To unset the session variable if present
//including header.php 
include '../../include/header.php';
//including page.php (It is a class which contain functions)
include '../../libraries/page.php';
//created object for page.php
$page = new page();
session_start();
unset($_SESSION['display']);
unset($_SESSION['total']);
unset($_SESSION['customer']);
unset($_SESSION['balance']);
 
$userid = $_SESSION['userid'];

//To get current date and time
date_default_timezone_set('Asia/Calcutta');
$date1 = date("Y-m-d H:i:s");

// checking the session
if (!$_SESSION['username']) {
    header("location:../user/login.php");
}
if ($_GET['status'] == '1') {
    $msg = $message[13];
} else if ($_GET['status'] == '0') {
    $msg = $message[0];
}
if ($_GET['q'] == 'logout') {
    $page->user_logout();
    header("location:../user/login.php");
}
if (isset($_POST['add'])) {
    if (isset($_POST['productid']) && isset($_POST['qty'])) { # Find out if the form had been submitted
        session_start();

        if (isset($_POST['display'])) {
            $display = unserialize(base64_decode($_POST['display']));
            array_push($display, array("productid" => $_POST['productid'], "qty" => $_POST['qty'], "discount" => $_POST['discount']));
        } else {
            $display = array();
            //$display[0] = $_POST;

            $display[0] = array("productid" => $_POST['productid'], "qty" => $_POST['qty'], "discount" => $_POST['discount']);
            //print_r($display);
        }
//$total_vat=$_POST['vat'];
    }
}


?>

<!------------------css style sheets for responsive tables------------------------------------>
<link rel="stylesheet" type="text/css" href="../../css/add_sales_billing/style_table.css">
<link rel="stylesheet" type="text/css" href="../../css/add_sales_billing/responsive_table.css">
<link rel="stylesheet" type="text/css" href="../../css/sales.css">

<!-------------------------------------Navbar---------------------------------->
<?
$stock_register = 'active';
include '../../include/navbar.php';
?>

<div class="bread_crumb" style="margin-left: 2%">
    &laquo;  <a href="sales_billing">Back</a>
  </div>
<!----------------------------------Display the result message of Addition--------------------->
<div  align="center"  id="save_reply"><? echo $msg; ?></div>

  
  <script type="text/javascript">
    $(function(){
    $("#emailForm").submit(function(){
    dataString = $("#emailForm").serialize();
    $.ajax({
    type: "POST",
    url: "post.php",
    data: dataString,
    bill: dataString,
    dataType: "json",
    timeout: 5000,
    error: function (xhr, err) {
    $("#msg").html(xhr.responseText + xhr.readyState + xhr.status);
    },
    success: function(data) {
         $("#save_reply").html(data[2].mesg);
      $('#date').val(data[1].date);
        $('#total_rate').val(data[1].total_rate);
        $('#total_discount').val(data[1].total_discount);
        $('#total_vat').val(data[1].total_vat);
        $("#bill_total").val(data[1].bill_total);
        $("#msg").html("");
        for(var i=0; i<data[0].length;) {
           
  
     $('<tr>').html("<td><input type='checkbox' name='chkDel[]' id='chkDel"+i+"' value='"+i+"'></td><td>&nbsp;</td><td>"+data[0][i]['name']+"</td><td>"+data[0][i]['price']+"</td><td>"+data[0][i]['qty']+"</td><td>"+data[0][i]['vat']+"</td><td>"+data[0][i]['discount_product_total']+"</td><td>"+data[0][i]['amount']+"</td></tr>").appendTo('#msg');
    i++;
        }
    }
    });
    return false;
    });
    });
    </script>
   
   <script>
function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","show_qty.php?q="+str,true);
xmlhttp.send();
}
</script>
   
  
     
  <script type="text/javascript">
    $(function(){
    $("#myForm").submit(function(){
    dataString = $("#myForm").serialize();
    $.ajax({
    type: "POST",
    url: "delete.php",
    data: dataString,
    bill: dataString,
    dataType: "json",
    timeout: 5000,
      error: function (xhr, err) {
    $("#msg").html(xhr.responseText + xhr.readyState + xhr.status);
    },
    success: function(data) {
      $('#date').val(data[1].date);
        $('#total_rate').val(data[1].total_rate);
        $('#total_discount').val(data[1].total_discount);
        $('#total_vat').val(data[1].total_vat);
        $("#bill_total").val(data[1].bill_total);
        $("#msg").html("");
        for(var i=0; i<data[0].length;) {
           
  
    $('<tr>').html("<td><input type='checkbox' name='chkDel[]' id='chkDel"+i+"' value='"+i+"'></td><td>&nbsp;</td><td>"+data[0][i]['productid']+"</td><td>"+data[0][i]['price']+"</td><td>"+data[0][i]['qty']+"</td><td>"+data[0][i]['vat']+"</td><td>"+data[0][i]['discount_product_total']+"</td><td>"+data[0][i]['amount']+"</td><td></td></tr>").appendTo('#msg');
    i++;
        }
    }
    });
    return false;
    });
    });
    </script>
   
   
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script>                          
                function myCall() {
                    var request = $.ajax({
                        url: "save_bill.php",
                        type: "POST",            
                        dataType: "html"
                    });
 
                    request.done(function(msg) {
                        $("#save_reply").html(msg);          
                    });
 
                    request.fail(function(jqXHR, textStatus) {
                        alert( "Request failed: " + textStatus );
                    });
                }
             
        </script>
  <script>
function customer(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("save_reply").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","customer_select.php?q="+str,true);
xmlhttp.send();
}
</script>

<!---------------------------------form1------------------------------------------------>
        <form>

    <div class="maindiv3" >
         <h3 style="color:#777;text-align: center; font-size: 18px">Billing</h3>
        <div class="sales_leftdiv" style="clear:both">
            <label>Bill Date</label>
            <input class="input-xlarge"  id="date" type="text"  name ="bill_date" value="">
        </div>
        <div class="sales_middiv"></div>
        <div class="sales_leftdiv">
            <label>Bill to</label>
                
             <form>
                 <select name="customers" style="width:280px" onchange="customer(this.value)"><option></option> 
                
                <?
                $customers = mysql_query("select * from customers");
                $id = 0;
                while ($customers_data = mysql_fetch_array($customers)) {
                ?>
                <option value="<?= $customers_data['customerid']; ?>"><?= $customers_data['customer']; ?></option>
                <?
                }
                ?>


            </select>
             </form>
          
        </div>
        <div class="sales_middiv"></div>
        <div class="sales_leftdiv">
            <label>Total</label>
            <input class="input-xlarge" id="total_rate" type="text" name ="total_rate"    readonly>
        </div>

        <div class="sales_leftdiv">
            <label>Discount</label>
            <input class="input-xlarge" id="total_discount" type="number" name ="total_discount"   readonly>
        </div>
        <div class="middiv"></div>
        <div class="sales_leftdiv">
            <label>VAT @14%</label>
            <input class="input-xlarge" id="total_vat" type="text" name ="total_vat" value=""  readonly>
        </div>
        <div class="middiv"></div>
        <div class="sales_leftdiv">
            <label>Bill Total</label>
            <input class="input-xlarge" id="bill_total" type="text" name ="bill_total"  readonly >

        </div>


    </div>
</form>
    
    <!---------------------------------form2------------------------------------------------>
    
<div >
    <form id="emailForm">
        <div class="maindiv3" >
            <h3 style="color:#777;text-align: center; font-size: 18px">Select Item</h3>
            <div class="sales_leftdiv">
                <label>Product</label>
                <select name="productid" style="width:290px" required>
                    <option></option>
                    <?
                     $invoice_detail = mysql_query("select productid from invoicedetails where (qty+free)-sold > '0'");
                    while ($invoice_details_data = mysql_fetch_array($invoice_detail)) {
                    $invoice_product_id=$invoice_details_data['productid'];
                    $products = mysql_query("select productid,name from products where productid=$invoice_product_id");
                    while ($products_data = mysql_fetch_array($products)) {
                    ?>
                    <option value="<?= $products_data['productid']; ?>"><?= $products_data['name']; ?></option>
                    <?
                    }
                    }
                    ?>
                </select>
            </div>
            <div class="sales_middiv"></div>
            <div class="sales_leftdiv">
                <label>Qty</label>
                <input class="input-xlarge" id="focusedInput" type="number"  name ="qty" value="1"  onkeyup="showHint(this.value)">

<!--<input type="hidden" name="vat" value="<?//=$total_vat; ?>">-->
            </div>
            <div class="sales_leftdiv">
                <label>Discount</label>
                <input class="input-xlarge" id="focusedInput" type="number"  name ="discount" value="0" >
            </div>
            <div class="sales_rightdiv">
                <div class="btn1"><button class="btn btn-inverse"  type="submit" name="add" >Add</button></div>
            </div>
            <div id="content">

            </div>

        </div>
    </form>
</div>

    <!---------------------------------form3------------------------------------------------>
    
    <div>


<form id="myForm">
    <div style="clear:both"></div>
    <!-------------------------------------------Group selection options----------------->
    <div class="group_select_desk" style="clear:both">
        <span><strong style="font-size:18px;">Billing Items</strong>
         </span>


        <button class="btn" type="submit" name="delete" style="float:right;margin-right: 10px"><i class="icon-trash"></i>Delete</button>
        <div class="btn-group"  style="float: right;margin-right: 10px">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Check&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="javascript:allCheck('chkDel[]',true)">Select All</a></li>
                <li><a href="javascript:allCheck('chkDel[]',false)">Select None</a></li> 
                <li><a href="javascript:reserveCheck('chkDel[]')" >Invert Select</a></li>
            </ul>
        </div>
        <button class="btn btn-success" type="submit" name="save"  style="float:right;margin-right:10px" onclick="myCall()">Save Bill</button>
        <a href ="add_stock.php" style="float:right;margin-right: 10px"><button class="btn" type="button"><i class="icon-file"></i>New</button></a>
    </div>

    <div class="group_select_mob" style="clear:both;">
        <span style="width:100%;"><strong style="font-size:22px;">Billing List</strong></span><br><br>

        <div class="btn-group"  style="float: left">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Select&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="javascript:allCheck('chkDel[]',true)">Select All</a></li>
                <li><a href="javascript:allCheck('chkDel[]',false)">Select None</a></li> 
                <li><a href="javascript:reserveCheck('chkDel[]')" >Invert Select</a></li>   
            </ul>
        </div>
        <div class="btn-group"  style="float: right">
            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#" >
                Action&nbsp;
                <span class="caret"></span>
            </a>
            <ul class="dropdown-menu"  style="float:left;margin-left: -84px">
                <!-- dropdown menu links -->
                <li><a href="add_sales_billing.php"><i class="icon-file"></i>New</a></li> 
                <li>

                    <button class="btn btn-success" type="submit" name="save" onclick="javascript: form.action = '../../libraries/save_sales_billing';" style="width:100%">Save Bill</button></li>
        </div>
    </div>

    <br />


    <!------------------------------Details Table------------------------------------->
    <table class="table1 table-striped" style="margin-left: auto;margin-right: auto;margin-top: 15px; ">
        <thead  class="thead1">
            <tr>
                <th></th>
                <th>SL NO</th>
                <th>Product Name</th>
                <th>Rate</th>
                <th>QTY</th>
                <th>TAX</th>
                <th>Discount</th>
                <th>Amount</th>
            </tr>
         
        </thead>
        <tbody id="msg">

                <!-------------------------Dynamic row to display from database--------------------->
                  
 
        </tbody>
    </table>

    
</form>
</div>


       <div id="dup">
        
        
    </div>
    
<?php
//including footer.php
include '../../include/footer.php';
?>

